var user_id = localStorage.getItem("user_id"); //user_id
var is_this;
mui.init({
	swipeBack: false,
	//下拉刷新
	pullRefresh: {
		container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up: {
			height: 50, // 可选.默认50.触发上拉加载拖动距离
			auto: true, // 可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh //加载数据方法 
		}
	}
});

//执行刷新
window.addEventListener('refresh_method', function(e) {
	location.reload();
});

function recommend() { //推荐列表
	var number = $("#number").val();
	user_id = user_id ? user_id : '';
	interfRequest.recommend.data = {
		number: number,
		pagesize: 5,
		lat: "",
		lon: "",
		user_id: user_id
	}
	socialRequest(interfRequest.recommend); //异常处理
}

//下拉加载
function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
		setTimeout(function() {
			var isOver = $("#isOver").val();
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。  
			recommend(); //请求数据
		}, 500);
	}, 500);
}

//查看用户
mui('body').on('tap', '.recommend_top img', function() {
	var mag = $("#mag").val(); //提示  
	var id = $(this).siblings().find(".nickname").attr("id");
	//var mag = $("#mag").val();//提示  
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		mui.openWindow({
			url: 'html/video/fan_pages.html',
			extras: {
				publisher: id,
			},
			createNew: true
		});
	}
})

//点赞动态
mui('body').on('tap', '.zan', function() {
	is_this = $(this);
	var mag = $("#mag").val(); //提示  
	var dynamic_id = $(this).parents(".recommend_list").attr("id");
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		interfRequest.dynamic_label.data = {
			user_id: user_id,
			dynamic_id: dynamic_id, //true	普通参数	int		动态id
		}
		socialRequest(interfRequest.dynamic_label); //异常处理 

	}

});

/*点赞*/
function dynamic_labelHandle(data) {
	if(data.error_code == 10000) {
		is_this.find(".iconfont").addClass("lover");
		var dz_num = is_this.find(".iconfont").siblings().text();
		is_this.find(".iconfont").siblings().text(parseInt(dz_num) + 1);
	} else {
		$("#zan").val(data.error_code);
		//mui.toast("该条动态已赞");   
	}
}

//查看详情     评论 
mui('body').on('tap', '.content_text,.comment', function() {
	var mag = $("#mag").val(); //提示  
	var id = $(this).parents(".recommend_list").attr("id"); //动态id
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		mui.openWindow({
			url: 'html/index/graphic_details_page.html',
			id: 'graphic_details_page',
			extras: {
				dynamic_id: id,
			},
			createNew: true
		});
	}
});

//更多
mui('body').on('tap', '.more', function() {
	is_this = $(this); //
	//$("#report").attr("sta",id);
});

//举报
mui('body').on('tap', '#report', function() {
	var mag = $("#mag").val(); //提示  
	var dynamicid = is_this.parents(".recommend_list").attr("id"); //动态id
	var passiveid = is_this.parent().siblings(".recommend_top").find(".nickname").attr("id"); //动态用户id
	var username = is_this.parent().siblings(".recommend_top").find(".nickname").text(); //动态用户昵称 
	if(mag == "请先登录") { //判断是否登录  
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		mui.openWindow({
			url: 'html/index/Report.html',
			id: 'Report',
			extras: {
				dynamic_id: dynamicid,
				passive_id: passiveid,
				passive_username: username
			},
			createNew: true
		});
	}
});

//图片预览
mui.previewImage();

//头部切换
mui('body').on('tap', '.tad_title', function() {
	window.top.location.href = this.href;
});

//底部菜单
mui('body').on('tap', '.mui-tab-item', function() {
	window.top.location.href = this.href;
});

//消息
mui('body').on('tap', '#my_news', function() {
	var mag = $("#mag").val(); //提示  
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		window.top.location.href = "html/news/fx_message.html";
	}
});

//我的
mui('body').on('tap', '#user', function() {
	var mag = $("#mag").val(); //提示  
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else {
		window.top.location.href = "html/my/my.html";
	}
});

//发布动态
mui('body').on('tap', '.public_menu_img a', function() {
	var sta = $(this).attr("sta");
	var mag = $("#mag").val(); //提示  
	if(mag == "请先登录") { //判断是否登录 
		var btnArray = ['否', '是'];
		mui.confirm(mag, '提示', btnArray, function(e) {
			if(e.index == 1) {
				/*mui.openWindow({
					url: 'html/user/login.html',
					id: 'login',
					createNew: true
				});*/
				window.top.location.href = 'html/user/login.html';
			} else {}
		})
	} else if(sta == 1) { //发布图文
		mui.openWindow({
			url: 'html/index/issued_by.html',
			id: 'issued_by',
			createNew: true
		});
	} else if(sta == 2) { //发布视频
		mui.openWindow({
			url: 'html/index/issued_video.html',
			id: 'issued_video',
			createNew: true
		});
	}

});

//用户信息
function user_info() {
	interfRequest.user_info.data = {
		refresh: 1 //true	普通参数	string		刷新状态1代表刷新
	}
	socialRequest(interfRequest.user_info); //异常处理 
}

//视频播放
mui('body').on('tap', '.img_video', function() {
	var videoMedia = $(this).siblings().children("source").attr("src");
	var bu = document.getElementById("videoMedia"); 
	bu.src = videoMedia;
	bu.play();  
	$(".video_ji").show();  
	//FullScreen(); //全屏
});

//进入全屏
/*function FullScreen() {
    var ele = document.getElementById("videoMedia");
    if (ele .requestFullscreen) {
        ele .requestFullscreen();
    } else if (ele .mozRequestFullScreen) {
        ele .mozRequestFullScreen();
    } else if (ele .webkitRequestFullScreen) {
        ele .webkitRequestFullScreen();
    }
}*/

//关闭视频
mui('body').on('tap', '.back', function() {
	$(".video_ji").hide();
	var bu = document.getElementById("videoMedia");
	bu.innerHTML = '';
	bu.pause();
});


//搜索
mui('body').on('tap', '#recommend_search', function() {
	window.top.location.href = "html/index/landing_page0.html"
});

/*mui('body').on('tap', '#recommend_search', function() {
	window.top.location.href = "html/video/video_videoplayer.html"
});*/
//二级按钮退出APP
var first = null;
mui.back = function() {
	//	首次按键，提示‘再按一次退出应用’
	if(!first) {
		first = new Date().getTime();
		mui.toast('再按一次退出应用');
		setTimeout(function() {
			first = null;
		}, 1000);
	} else {
		if(new Date().getTime() - first < 1000) {
			plus.runtime.quit();
		}
	}
};

//热跟新
function findNewEdition() {
	mui.ajax('http://39.98.238.13/social-client/api/open/hotPatching', {
		data: {},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.error_code == 0) {
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					if(data.data) {
						$("#version_num").html(data.data.version_number); //升级版本
						$("#version_cont").html(data.data.chinese_copywriting); //状态
						var newVer = data.data.version_number.split(".").join(""); //新版本
						var oldVer = inf.version.split(".").join(""); //当前版本 
						if(Number(oldVer) < Number(newVer)) {
							$("#regenxin").attr("data-url", data.data.patch); //更新地址
							$(".mui-backdrop").show(); //显示更新窗口
						}　　
					}　　　　　　　
				});
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	});
}

//下载安装包
function downWgt(downloadWgtUrl) {
	plus.nativeUI.showWaiting("正在更新资源...");
	plus.downloader.createDownload(downloadWgtUrl, {
		filename: "_doc/update/"
	}, function(d, status) {
		if(status == 200) {
			console.log("下载wgt成功：" + d.filename);
			installWgt(d.filename); // 安装wgt包 
		} else {
			mui.toast("下载wgt失败！");
		}
		plus.nativeUI.closeWaiting();
	}).start();
}

// 更新应用资源升级包 
function installWgt(path) {
	plus.nativeUI.showWaiting("安装文件...");
	plus.runtime.install(path, {}, function() {
		plus.nativeUI.closeWaiting();
		plus.nativeUI.alert("应用资源更新完成！", function() {
			plus.runtime.restart(); //重启
		});
	}, function(e) {
		plus.nativeUI.closeWaiting();
		plus.nativeUI.alert(JSON.stringify(e));
	});
}

//立即更新
$(document).on('tap', '#regenxin', function() {
	downWgt($("#regenxin").attr("data-url")); //下载安装包
})

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		user_info(); //用户信息
		findNewEdition(); //热跟新
		//ios禁止右滑
		var wv = plus.webview.currentWebview();
		// 关闭侧滑返回功能
		wv.setStyle({
			'popGesture': 'none'
		});
	});
}